import axios from 'axios';
import 'vant/lib/dialog/style';
import 'vant/lib/toast/style';
import jsonpAdapter from '../utils/axios_jsonp.js';
import { GlobalModule } from '@/store/modules/global';
import {  Vue } from 'vue-property-decorator';
import { Alert } from '@/utils';
import { Dialog ,Toast } from 'vant';
import router from '../router/index';
Vue.use(Dialog);
Vue.use(Toast);
const app = require('../../app.json');
const baseURL = process.env.NODE_ENV === 'production' ? app.url.prd : app.url.dev;
// const token = sessionStorage.getItem('token');
export default class BaseApi {
  constructor() {
    this.axios = axios.create({
      baseURL: baseURL,
      timeout: 1000 * 60,
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
        // TOKEN: '',
        // SYSTEMSOURCE: 'W01',
      },
    });
    // 增加请求拦截器
    this.axios.interceptors.request.use(
      (config: any) => {
      // Toast.loading({
      //   duration: 0,
      //   message: ' 请耐心等待  正在努力加载',
      //   forbidClick: true,
      //    icon: 'https://wechat.chinalife-p.com.cn/viewimages/kzimages/images/loading300.gif',
      //  });
      Toast.loading({
        duration: 0,
        message: '加载中...',
        forbidClick: true,
       });
        // let token = sessionStorage.getItem('token');
        // GlobalModule.pendingPlus();
        // if (token) {
        //   config.headers.TOKEN = token;
        //   // console.log('request=====>', config);
        // }
        return config;
      },
      (error: any) => {
        GlobalModule.pendingMinus();
        Dialog.alert({ title: '温馨提示', message: error.toString() });
        return {};
      },
    );
    // 增加返回拦截器
    this.axios.interceptors.response.use(
      (resp: any) => {
        Toast.clear();
        GlobalModule.pendingMinus();
        // if (resp.data.responseHeadDto.responseCode === '1002' || resp.data.responseHeadDto.responseCode === '1003') {
        //   sessionStorage.removeItem('token');
        //   alert(JSON.stringify('此链接已失效，请重新扫二维码进入!'));
        //   router.push({ name: 'black' });
        //   return;
        // }
        // console.log('resp=====>', resp);
        return resp;
      },
      (error: any) => {
        GlobalModule.pendingMinus();
        Toast('网络请求不给力，请稍后重试'); 
        // Dialog.alert({ title: '温馨提示', message: '网络请求不给力，请稍后重试' });
        return {};
      },
    );
  }

  async post(url: string, data: any) {
    return await this.axios.post(url, data);
  }

  async get(url: string, params: any) {
    return await this.axios.get(url, {
      params,
    });
  }

  async jsonp(url: string, params: any) {
    return await this.axios({
      url: url,
      params: params,
      adapter: jsonpAdapter,
    });
  }
  private axios: any;
}
